package com.karaoke.android.kege.vm

import android.databinding.ObservableField
import com.karaoke.android.data.NetworkTools
import com.karaoke.android.data.domain.QSong
import com.karaoke.android.data.domain.Singer
import com.karaoke.android.data.onIO
import com.karaoke.android.data.onUI
import com.trello.rxlifecycle.kotlin.bindToLifecycle
import rx.Observable
import java.util.concurrent.TimeUnit

/**
 * Created by allen on 2017/4/22.
 */

class SingerDetailViewModel(private val s: Singer) : BaseSongViewModel()
{
    override val emptyString: String? = "歌星暂无歌曲"
    override val errorString: String? = "加载失败"

    val transitionName = "singer_${s.id}"
    val singer = ObservableField<Singer>().apply { set(s) }

    override fun onInitLoad()
    {
        super.onInitLoad()
        qSong.apply {
            queryType = QSong.QueryType.QUERY_SINGER
            criteria = s.id
            size = 60
        }
        state?.showLoading("加载中...")
        canLoadMore = true
        Observable.timer(250, TimeUnit.MILLISECONDS)
                .onIO()
                .bindToLifecycle(this)
                .flatMap {
                    NetworkTools.singerService.getSinger(s.id)
                }.onUI({
                           singer.set(it)
                           onLoad()
                       }, {})
    }
}
